<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>微信登录回调页面</title>
    <script src="jquery-1.9.1.js"></script>
    <style type="text/css">
    </style>
</head>
<body>

<div class="load-div">
    <h2>登录微信回调页面</h2>
</div>
</body>
</html>
<script>
    setTimeout(function(){getTeacherAndGoHomePage();}, 400);
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return (false);
    }

    function setCookie(name, value) {
        var Days = 30;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }

    //getTeacher
    function getTeacherAndGoHomePage() {
        //去后端获取weixinUserId，并初始化数据
        var code = getQueryVariable("code");
        setCookie("code", code);
        getLoginUserInfoByWeixinCodeAndGoHomePage(code);
    }

    function getLoginUserInfoByWeixinCodeAndGoHomePage(code) {
        $.ajax({
                   url: "user/getLoginUserInfoByWeixinCode",
                   type: "get",
                   data: {
                       code: code
                   },
                   dataType: "json",
                   success: function (responseJsonStr) {
                       if (!commomCheckLogin(responseJsonStr)) {
                           return false;
                       }
                       selectOuId(responseJsonStr.data);//选择学校
                   },
                   error: function (error) {
                       document.write(" 登录失败");
                   }
               });
    }

    //应用后台通用结果处理
    function commomCheckLogin(responseJsonStr) {
        if (responseJsonStr.status == 1403) {
            document.write(responseJsonStr.message);
            return false;
        } else if (responseJsonStr.status == 1401) {
            goIntoPageBeforeAuthAll();
            return false;
        } else {
            return true;
        }
    }

    //看有不有兼职，有就选，没有就默认
    function selectOuId(responseJsonStr) {
        //将weixinUserId设置在cookie里,如果不存在，则提示，不进行后续操作
        //将teacherId设置在cookie里,如果不存在，则提示，不进行后续操作
        //确定schoolId和ouId,并设置在cookie里，如果不确定，需要在界面选择
        setCookie("qyWeixinUserIdToken", responseJsonStr.wxUserId);
        setCookie("teacherIdToken", responseJsonStr.teacherId);
        setCookie("wxToken", responseJsonStr.wxToken);//

        if (responseJsonStr.orgUserVoList != null
            && responseJsonStr.orgUserVoList.length != 0) {
            //弹窗选择-----模拟
            //选择完成
            setCookie("ouIdToken", responseJsonStr.orgUserVoList[0].ouId);
            setCookie("schoolIdToken", responseJsonStr.orgUserVoList[0].schoolId);
            setCookie("schoolZoneIdToken", responseJsonStr.orgUserVoList[0].schoolZoneId);
        } else {
            setCookie("schoolIdToken", responseJsonStr.schoolId);
            setCookie("ouIdToken", responseJsonStr.ouId);
            setCookie("schoolZoneIdToken", responseJsonStr.schoolZoneId);
        }
        goIntoPageAfterAuthAll();//微信和应用端完全授权后进入首页
    }

    //微信和应用端完全授权后进入首页
    function goIntoPageAfterAuthAll() {
        var h = window.location.href.split("/afterLoginWeixin.html")[0];
        window.location.href = h + "/index.html";
    }
    function goIntoPageBeforeAuthAll() {
        var h = window.location.href.split("/afterLoginWeixin.html")[0];
        window.location.href = h + "/beforeLoginWeixin.html";
    }
</script>